#include<stdio.h>
#include<stdlib.h>//包含常用的内存操作函数，如malloc、free等，及数值计算函数，如abs、pow等。
#include "seqTable.h"
#include <string.h>

//创建顺序表
SEQtable_t* createSeqTable(int n) {
      SEQtable_t* table=NULL;
      table = malloc(sizeof(SEQtable_t));//为表头分配内存
      if (table == NULL) { //如果表头分配失败
          //使用fprintf函数将错误信息输出到标准错误流(stderr)
         fprintf(stderr, "Error allocating memory for SEQtable_t\n");
          return NULL;
      }
      table->data = malloc(sizeof(int) * n);//为数据分配内存
      if (table->data == NULL) {//如果数据分配失败
         fprintf(stderr, "Error allocating memory for SEQtable_t->data\n");
          free(table); //释放表头内存
          return NULL;
      }
    table->pos = 0; //初始化数据位置
    table->capacity = n; //初始化容量
     return table;
}

//释放顺序表
void releaseSeqTable(SEQtable_t* table) {
    if (table != NULL) {//如果表不为空
        if (table->data != NULL) free(table->data);//如果数据不为空，释放数据内存
        free(table);//释放表头内存
        table = NULL; //将表指针置空
    }
}

//扩容函数
//使用static修饰的函数只能在当前文件中使用，不能在其他文件中使用
static int enlargerTable(SEQtable_t* table) {
    //申请一个新空间，大小为原来的2倍
   Element_t* temp=malloc(sizeof(Element_t) * table->capacity * 2);//为数据分配内存
   if (temp == NULL) {
       fprintf(stderr, "Error allocating memory for enlargerTable\n");
       return -1;
   }
    //将原来的数据复制到新空间
    memcpy(temp, table->data, sizeof(Element_t) * table->capacity);
    //更新表头，释放原来的数据空间
    table->capacity *= 2;
    free(table->data);//释放原来的数据空间
    table->data = temp;//更新数据指针
    return 0;
}

//尾插函数
int pushBackSeqTable(SEQtable_t *table, Element_t value) {
    if (table == NULL) {
        fprintf(stderr, "Error: table is NULL\n");
        return -1;
    }
    //顺序表已满，且扩容失败
    if (table->pos >= table->capacity && enlargerTable(table) != 0) {
            fprintf(stderr, "Error: enlargerTable failed\n");
            return -1;
    }

    table->data[table->pos] = value;
    ++table->pos;
    return 0;

}

//指定位置插入函数
int  insertPosSeqTable(SEQtable_t *table, int index, Element_t value) {
    if (table == NULL) {
            fprintf(stderr, "Error: table is NULL\n");
            return -1;
    }
    //插入位置合法判断
    if (index < 0||index > table->pos) {
        printf("Error: index out of range\n");
        return -1;
    }
    //顺序表已满，且扩容失败
    if (table->pos>= table->capacity && enlargerTable(table) != 0) {
        fprintf(stderr, "Error: enlargerTable failed\n");
        return -1;
    }
    //将插入位置后面的元素后移
   for (int i=table->pos;i>index;--i) table->data[i] = table->data[i-1];
    //插入元素
    table->data[index] = value;
    ++table->pos;
    return 0;
}

//删除元素值函数
int deleteSeqTable(SEQtable_t *table, Element_t value) {
    if (table == NULL) {fprintf(stderr, "Error: table is NULL\n"); return -1;}
    int index = findSeqTable(table, value);
    if (index==-1) { printf("Error: index out of range\n"); return -1;}
    for (int i=index;i<table->pos-1;++i) table->data[i] = table->data[i+1];
    --table->pos;
    return 0;
}

// 查找元素值位置函数
int findSeqTable(const SEQtable_t *table, Element_t value) {
    if (table == NULL) {fprintf(stderr, "Error: table is NULL\n"); return -1;}
    if (table->pos == 0) {fprintf(stderr, "Error: table is empty\n"); return -1;}
    for (int i=0;i<table->pos;i++) {if (table->data[i]==value) return i;}
    return -1;//未找到返回-1
}

void showSeqTable(const SEQtable_t *table) {
    if (table == NULL) {
            fprintf(stderr, "Error: table is NULL\n");
            return;
    }
    if (table->pos == 0) {
        printf("table is empty\n");
        return;
    }

    for (int i = 0; i < table->pos; ++i) {
        printf("%d\t", table->data[i]);
    }
    printf("\n");
}
